<?php
//借还记录管理
class BorrowAction extends CommonAction {
    function _filter(&$map) {
        if(!empty($_REQUEST['keywords']) ){
            $key = $_REQUEST['keywords'];
            //$map['id'] = array('egt',2);
            $map['name'] = array('like',"%".$key."%");//物品名称
            //按会员名，或姓名 或房间号查询，先从入住单中查出对应的单ID
            $con = array(
                'member_name' => array('like',"%".$key."%"),//会员名或帐号名
                'person_name' => array('like',"%".$key."%"),//入住人
                'room_no' => array('like',"%".$key."%"), //房间号
                '_logic' => "or",
            );
             $checkin_ids = M('Checkin')->where($con)->field('id')->findAll();
             $checkins = array();
             foreach ($checkin_ids as $k1 => $value) {
                 $checkins[$k1] = $value['id'];
             }
             $check_id = implode(",", $checkins);
             if(!empty($checkin_ids)) {
                 $map['checkin_id'] = array('in',$check_id);
             }
            $map['_logic'] = "or";
        }
    }

    function _before_index(){
       $this->assign('status',C('borrowStatus'));
    }

    function _before_add(){
        $this->assign('status',C('borrowstatus'));
        $this->assign('checkins',M('Checkin')->where('status > 0')->order('create_time desc')->limit(20)->findAll() );//暂时取最近的20个入住消费单TODO
    }

    function _before_edit(){
        $this->assign('status',C('borrowstatus'));
    }
    /**
     * 增加借还记录
     */
    public function insert(){
        $data = $_POST;
        $model = D('Borrow');
        $data['start_time'] = empty($_POST['start_time'])?0:strtotime( $_POST['start_time'] );
        $data['end_time'] =empty($_POST['end_time'])?0:strtotime( $_POST['end_time'] );
        $data['create_time'] = time();
        $data['user_id'] = $_SESSION[C('USER_AUTH_KEY')];
        $jumpUrl = $_REQUEST['jumpUrl'];
        if(false == $model ->create()){
            $this->error($model -> getError());
        }
        if(false !== $list = $model ->add($data)){
            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );
            $this-> success("借还记录新增成功！");
        }
        else{
              $this->error('新增失败！');
        }
        
    }

    public function update(){
        $data = $_POST;
        if(!empty($_POST['start_time'])){
            $start_time = $_POST['start_time'];
             if(!strpos($_POST['start_time'],":" ) ){//没有时间
                 $nowTime = date("H:i:s");
                 $start_time .= " ".$nowTime;
             }
        }
        
        if(!empty($_POST['end_time'])){
            $end_time = $_POST['end_time'];
             if(!strpos( $_POST['end_time'],":" ) ){//没有时间
                 $nowTime = date("H:i:s");
                 $end_time .= " ".$nowTime;
             }
        }
        
        $jumpUrl = $_REQUEST['jumpUrl'];
       $data['start_time'] = empty($_POST['start_time'])?0:strtotime( $start_time );
        $data['end_time'] =empty($_POST['end_time'])?0:strtotime( $end_time );
        $data['create_time'] = empty($_POST['create_time'])?0:strtotime( $_POST['create_time'] );//保持原来的创建时间
        $data['update_time'] = time();
        $data['user_id'] = $_SESSION[C('USER_AUTH_KEY')];
        $model = D('Borrow');
        if(false == $model ->create()){
            $this->error($model -> getError());
        }
        if(false !== $list = $model ->save($data)){
            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );
            $this-> success("借还记录修改成功！");
        }
        else{
             $this->error("借还记录修改失败！");
        }
    }
}
?>